package com.ppl.server.modules;

import akka.actor.ActorSystem;
import com.google.inject.AbstractModule;
import com.ppl.server.boot.Boot;
import com.ppl.server.boot.HttpConnector;
import com.sun.jersey.api.container.ContainerFactory;
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;
import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerRequestFilter;
import com.typesafe.config.ConfigFactory;
import org.glassfish.grizzly.http.server.HttpHandler;

/**
 * User: Vincent
 * Date: 11/13/12
 * Time: 5:43 PM
 */
public class Config extends AbstractModule {

    @Override
    protected void configure() {
        bind(ActorSystem.class).toInstance(Boot.actorSystem);

        bind(HttpConnector.class).to(HttpServerStartUp.class).asEagerSingleton();
        install(new DBModule());
        install(new CacheModule());
        com.typesafe.config.Config config = ConfigFactory.load();
        install(new ConfigModule(config));
        requestStaticInjection(Boot.class);
//        install(new ValidationModule());
    }


    public static class HttpServerStartUp extends HttpConnector{

        @Override
        protected HttpHandler createHttpHandler() {
            ResourceConfig config = new PackagesResourceConfig("com.ppl.server.resources");

            ContainerRequestFilter authFilter = new ContainerRequestFilter() {

                @Override
                public ContainerRequest filter(ContainerRequest request) {

                    return request;  //To change body of implemented methods use File | Settings | File Templates.
                }
            };
            config.getContainerRequestFilters().add(authFilter);

            return ContainerFactory.createContainer(HttpHandler.class, config);
        }
    }
}
